package io.finer.erp.jeecg.jolimark.print;

import io.finer.erp.jeecg.jolimark.enums.PrinterEnum;
import io.finer.erp.jeecg.jolimark.model.ExpressInformation;
import io.finer.erp.jeecg.jolimark.model.PrintExpressRequestModel;
import io.finer.erp.jeecg.jolimark.model.PrintRequestModel;

/**
 * 快递面单打印
 * 
 * @author Jolimark
 *
 */
public class ExpressPrint extends AbstractPrint {
	/**
	 * 打印快递面单
	 * <p>
	 * 适用场景：直接对接快递面单打印
	 * <p/>
	 * 
	 * <p>
	 * 适用机型：CFP-535、CFP-536、CFP-820
	 * </p>
	 * 
	 * @param printerEnum        打印机型号
	 * @param templateId         打印快递模板ID
	 * @param expressInformation 快递信息
	 * @param paperHeight        打印纸高度
	 */
	public void print(PrinterEnum printerEnum, String templateId, ExpressInformation expressInformation,
					  int paperHeight) {
		if (printerEnum.getType() != 2) {
			Out("错误：只支持CFP系列打印机");
			return;
		}
		PrintExpressRequestModel model = buildExpressRequest(printerEnum, templateId, expressInformation);
		model.setPaperHeight(paperHeight);

		print("PrintExpress", model);
	}

	/**
	 * 快递单打印不校验billContent
	 */
	@Override
	protected boolean billContentValidation(String billContent) {
		return true;
	}

	@Override
	protected String convert(PrintRequestModel model) {
		String baseString = super.convert(model);
		PrintExpressRequestModel expressModel = (PrintExpressRequestModel) model;
		StringBuilder result = new StringBuilder();
		result.append(baseString);
		result.append("&template_id=").append(expressModel.getTemplateId());
		if (expressModel.getExpressInformation() != null) {
			ExpressInformation expressInformation = expressModel.getExpressInformation();
			result.append("&jj_dwmc=").append(expressInformation.getShipperFrom());
			result.append("&jj_jjr=").append(expressInformation.getShipperContractPerson());
			result.append("&jj_lxdh=").append(expressInformation.getShippperContractPhone());
			result.append("&jj_dz=").append(expressInformation.getShippperAddress());
			result.append("&sj_dwmc=").append(expressInformation.getConsigneeTo());
			result.append("&sj_sjr=").append(expressInformation.getConsigneeContractPerson());
			result.append("&sj_lxdh=").append(expressInformation.getConsigneeContractPhone());
			result.append("&sj_dz=").append(expressInformation.getConsigneeAddress());
			result.append("&wp_jtw=").append(expressInformation.getDescriptionOfGoods());
			result.append("&wp_smjz=").append(expressInformation.getGoodsValue());
		}
		return result.toString();
	}

	private PrintExpressRequestModel buildExpressRequest(PrinterEnum printerEnum, String templateId,
			ExpressInformation expressInformation) {
		PrintRequestModel baseModel = buildRequest(printerEnum);
		PrintExpressRequestModel expressRequestModel = new PrintExpressRequestModel();
		expressRequestModel.setAccessToken(baseModel.getAccessToken());
		expressRequestModel.setAppId(baseModel.getAppId());
		expressRequestModel.setCopies(baseModel.getCopies());
		expressRequestModel.setCusOrderId(baseModel.getCusOrderId());
		expressRequestModel.setDeviceIds(baseModel.getDeviceIds());
		expressRequestModel.setPaperType(baseModel.getPaperType());
		expressRequestModel.setPaperWidth(baseModel.getPaperWidth());
		expressRequestModel.setTex(baseModel.getTex());
		expressRequestModel.setTimeout(baseModel.getTimeout());

		expressRequestModel.setExpressInformation(expressInformation);
		expressRequestModel.setTemplateId(templateId);

		return expressRequestModel;
	}
}
